package com.kx.center.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kx.center.entity.huiyin.TradePoi;
import com.kx.center.entity.query.QueryTrade;
import com.kx.center.service.TradeService;
import com.kx.center.util.DateUtil;
import com.kx.center.util.ExcelUtil;

@Controller
@RequestMapping("/statistical")
public class StatisticalController {
	
	@Autowired
	TradeService tradeService;
	@RequestMapping(value = "download", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public void download(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String fileName = "excel表格";

		Date from = null, to = null;
		QueryTrade queryTrade = new QueryTrade();
		String fromStr = request.getParameter("from");
		String toStr = request.getParameter("to");
		
		if(DateUtil.isDate(fromStr))
			from = DateUtils.parseDate(fromStr, "yyyy-MM-dd");

		if(DateUtil.isDate(toStr))
			to = DateUtils.parseDate(toStr + " 23:59:59", "yyyy-MM-dd HH:mm:ss");
		
		queryTrade.setFrom(from);
		queryTrade.setTo(to);

		List<TradePoi> tradePoi = tradeService.findTradeList(queryTrade);
		List<Map<String, Object>> list = tradeService.createExcelRecord(tradePoi);
		String TITLE[] = { "订单编号", "订单总金额", "运费", "实付价格", "数量", 
				"商品编码" , "商品名称", "支付时间", "门店名称", "渠道"};// 列名
		String keys[] = { "1", "2", "3", "4", "5", "6", "7" ,"8" ,"9" ,"10"};// map中的key
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		try {
			ExcelUtil.createWorkBook(list, keys, TITLE).write(os);
		} catch (IOException e) {
			e.printStackTrace();
		}
		byte[] content = os.toByteArray();
		InputStream is = new ByteArrayInputStream(content);
		response.reset();
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		response.setHeader("Content-Disposition", "attachment;filename="+ new String((fileName + ".xls").getBytes(), "iso-8859-1"));
		ServletOutputStream out = response.getOutputStream();
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			bis = new BufferedInputStream(is);
			bos = new BufferedOutputStream(out);
			byte[] buff = new byte[2048];
			int bytesRead;
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
			}
		} catch (final IOException e) {
			throw e;
		} finally {
			if (bis != null)
				bis.close();
			if (bos != null)
				bos.close();
		}
	}
}
